<?php

namespace App\Http\Controllers\Api\V1;

use SendSms;
use App\Http\Requests\SendSmsRequest;
use Dingo\Api\Exception\ResourceException;
use App\Http\Controllers\Controller;

class SendSmsController extends Controller
{
    public function store(SendSmsRequest $request)
    {
        $result = SendSms::key();
        return $this->response->array($result)->setStatusCode(201);

        // 服务容器写法
        $result = resolve('App\Repositories\KeyHouseRepository')->sendSms();
        return $this->response->array($result)->setStatusCode(201);

        // 下面代码移到了 仓库下面了
        $addMinutes = config('app.phone_addMinutes');

        if (config('app.env') == 'local') {
            $code = '123456';
        }else {
            // todo 发送验证码

        }
        $key = 'phone-'.str_random(15);

        $payload = \json_decode($request->payload, true);
        if(array_get($payload, 'payload.phone') != $request->phone) {
            throw new ResourceException('error',[['error_code'=>'430005','message'=>'手機號碼非法操作！']]);
        }
        $expiredAt = now()->addMinutes($addMinutes);

        \Cache::put($key, ['payload'=>$payload, 'code' => $code], $expiredAt);

        $result = [
            'phone_key' => $key,
            'expired_at' => $expiredAt->toDateTimeString(),
        ];

        return $this->response->array($result)->setStatusCode(201);

    }
}
